package com.ywy.controller;

import com.ywy.entity.Res;
import com.ywy.entity.User;
import com.ywy.service.ResService;
import com.ywy.utils.PageUtil;
import com.ywy.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 页面导航Controller
 *
 * @author ywy
 * @date 2022/8/17 11`:25
 */
@Controller
public class IndexController {
    @Resource
    private ResService resService;

    @Resource
    private RedisUtil<Integer> redisUtil;

    /**
     * 跳转到首页
     * @param session
     * @return
     */
    @RequestMapping("/")
    public ModelAndView index(HttpSession session) {
        session.setAttribute("typeId", "t_0");

        // 分页查询资源信息
        Res res = new Res();
        res.setCheckStatus(2); // 审核通过
        List<Res> resList = resService.list(res, 1, 20);
        Long total = resService.getTotal(res);

        // 热门资源
        res.setHot(true);
        List<Res> hotResList = resService.list(res, 1, 40);

        ModelAndView mav = new ModelAndView();
        mav.addObject("resList", resList);
        mav.addObject("pageCode", PageUtil.genPagination("/res/list", total, 1, 20, ""));
        mav.addObject("hotResList", hotResList);
        mav.setViewName("index");

        // 今日签到总数
        int signTotal = 0;
        try {
            if (redisUtil.hasKey("signTotal")) {
                signTotal = redisUtil.get("signTotal");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        session.getServletContext().setAttribute("signTotal", signTotal);

        return mav;
    }

    /**
     * 跳转到页面
     * @param pageName
     * @return
     */
    @RequestMapping("{pageName}.html")
    public ModelAndView toPage(@PathVariable("pageName") String pageName) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName(pageName);
        return mav;
    }

    /**
     * 跳转到页面
     * @param pageName
     * @return
     */
    @RequestMapping("{folder}/{pageName}.html")
    public ModelAndView toPage(@PathVariable("folder") String folder, @PathVariable("pageName") String pageName) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName(folder + "/" + pageName);
        return mav;
    }

    /**
     * 跳转到用户中心页面
     *
     * @return
     */
    @RequestMapping("center")
    public ModelAndView toUserCenterPage(HttpSession session) {
        User currentUser = (User) session.getAttribute("currentUser");
        // 查询链接资源无效的个数
        Res res = new Res();
        res.setUser(currentUser);
        res.setUseful(false);
        long total = resService.getTotal(res);

        ModelAndView mav = new ModelAndView();
        session.setAttribute("unUsefulResCount", total);
        mav.setViewName("user/userCenter");
        return mav;
    }
}
